"use strict"
const mvc = require("cosjs.mvc");
const opensdk = require('cosjs.loader')( mvc.config.get("root") + '/opensdk',true);

module.exports.handle = function() {
    let arr = Array.from(arguments);
    let key = this.opensdk["key"];
    let name = arr.shift();
    let sdk = opensdk.require(key);
    if(!sdk){
        return this.error('opensdk_not_exist',key);
    }
    if(typeof sdk[name] !=='function' ){
        return this.error('opensdk_gateway_not_exist',name);
    }
    return sdk[name].apply(this,arr);
};

module.exports.list = function() {
    let ret = {};
    opensdk.forEach((k,p)=>{
        let v = opensdk.require(k);
        if(v && v.format && typeof v.format === "object"){
            let id = v.format.id;
            ret[id] = v.format.name;
        }
    });
    return ret;
};

module.exports.fields = function(name) {
    let v = opensdk.require(name);
    if(v){
        return v.format.fields;
    }
    else {
        return null;
    }
};